﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Subsonic.Data;
using Subsonic.ViewModels;
using System;

namespace Subsonic.Controls
{
    public class MediaLongListSelector : LongListSelector
    {
        public MediaLongListSelector()
        {
            this.SelectionChanged +=new SelectionChangedEventHandler(MediaListBox_SelectionChanged);
        }

        void MediaListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.OnSelectionChanged(e);
        }
    }

    public class MediaListBox : ListBox
    {
        public MediaListBox()
        {
            this.SelectionChanged +=new SelectionChangedEventHandler(MediaListBox_SelectionChanged);
        }

        void MediaListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (SelectedIndex == -1)
            {
                return;
            }
            this.OnSelectionChanged(e);
            // Reset selected index to -1 (no selection)
            SelectedIndex = -1;
        }
    }

    public static class ListBoxExtensions
    {
        public static Page GetPage(this DependencyObject depObj)
        {
            DependencyObject current = depObj;
            while (!(current is Page))
            {
                current = VisualTreeHelper.GetParent(current);
            }
            return (Page)current;
        }

        public static void OnSelectionChanged(this DependencyObject depObj, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count != 1)
            {
                return;
            }

            ItemViewModel item = e.AddedItems[0] as ItemViewModel;

            if (item != null)
            {
                if (item is ChildItem)
                {
                    ChildItem child = (ChildItem)item;
                    if (!child.Child.IsDir)
                    {
                        // Play the album/list starting at the child.
                        if (depObj is Control && !(depObj.GetPage() is Subsonic.Pages.NowPlaying))
                        {
                            PivotItemViewModel pivm = ((Control)depObj).DataContext as PivotItemViewModel;
                            MusicPlayer.Instance.PlayList(pivm.Items.OfType<ChildItem>().Select(c => c.Child).ToList(), child.Child);
                            depObj.GetPage().NavigationService.Navigate(new Uri("/Pages/NowPlayingPivot.xaml", UriKind.Relative));
                        }
                    }
                }

                item.DoAction(depObj.GetPage().NavigationService);
            }
        }
    }
}
